package com.hw01.service.impl;

import java.time.LocalDateTime;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hw01.common.Result;
import com.hw01.common.ResultPage;
import com.hw01.common.UserInfo;
import com.hw01.constant.DelState;
import com.hw01.dto.TaskAllocationDto;
import com.hw01.dto.TaskAllocationQueryDto;
import com.hw01.entity.TaskAllocation;
import com.hw01.mapper.TaskAllocationMapper;
import com.hw01.service.TaskAllocationService;
import com.hw01.utils.DateUtils;
import com.hw01.vo.TaskAllocationVo;

/**
 * <p>
 * 分配任务 服务实现类
 * </p>
 *
 * @author zhong
 * @since 2024-03-18
 */
@Service
public class TaskAllocationServiceImpl extends ServiceImpl<TaskAllocationMapper, TaskAllocation> implements TaskAllocationService {

    @Autowired
    TaskAllocationMapper mapper;

    @Override
    public ResultPage<TaskAllocationVo> getTaskAllocationPage(TaskAllocationQueryDto dto) {

        if (dto == null) {
            dto = new TaskAllocationQueryDto();
        }

        if (StringUtils.isBlank(dto.getYear())) {
            dto.setYear(DateUtils.getYear());
        }
        dto.setMonth(DateUtils.mendMonth(dto.getMonth()));

        Page<TaskAllocationVo> page = ResultPage.toQueryPage(dto.getPageQuery());

        mapper.selectTaskAllocationPage(page, dto, UserInfo.getOrgId());

        return ResultPage.fromPage(page);
    }

    @Override
    public Result<Boolean> addOrUpdate(TaskAllocationDto dto) {
        if (dto == null) {
            return Result.error("缺少必要参数", Boolean.FALSE);
        }

        Integer workloads = dto.getWorkloads();
        if (workloads == null) {
            workloads = 100;
        }
        if (dto.getId() != null) {
            // === 修改
            TaskAllocation ta = mapper.selectById(dto.getId());
            if (ta == null || ta.getDelState()) {
                return Result.error("分配任务信息不存在，请刷新后重试", Boolean.FALSE);
            }

            LambdaUpdateWrapper<TaskAllocation> updateWrapper = new LambdaUpdateWrapper<>();

            if (Objects.equals(dto.getOperateType(), "1")) {
                updateWrapper.set(TaskAllocation::getWorkloads, workloads);
            } else {
                updateWrapper.set(TaskAllocation::getConfirmedWorkloads, dto.getWorkloads());
            }
            updateWrapper.set(TaskAllocation::getModifier, UserInfo.getUserId());
            updateWrapper.set(TaskAllocation::getModifyTm, LocalDateTime.now());
            updateWrapper.eq(TaskAllocation::getId, dto.getId());

            mapper.update(null, updateWrapper);

            return Result.success("修改分配任务成功", Boolean.TRUE);

        } else {
            // === 新增
            if (dto.getUserId() == null) {
                return Result.error("参数员工用户id不能为空", Boolean.FALSE);
            }

            if (StringUtils.isBlank(dto.getYear())) {
                return Result.error("参数年份不能为空", Boolean.FALSE);
            }

            if (StringUtils.isBlank(dto.getMonth())) {
                return Result.error("参数月份不能为空", Boolean.FALSE);
            }

            TaskAllocation ta = new TaskAllocation();
            ta.setUserId(dto.getUserId());
            ta.setYear(dto.getYear());
            ta.setMonth(DateUtils.mendMonth(dto.getMonth()));
            if (Objects.equals(dto.getOperateType(), "1")) {
                ta.setWorkloads(workloads);
            } else {
                ta.setConfirmedWorkloads(dto.getWorkloads());
            }
            ta.setCreator(UserInfo.getUserId());
            ta.setCreateTm(LocalDateTime.now());
            ta.setModifier(UserInfo.getUserId());
            ta.setModifyTm(LocalDateTime.now());
            ta.setDelState(DelState.UNDELETED);

            mapper.insert(ta);

            return Result.success("新增分配任务成功", Boolean.TRUE);
        }

    }
}
